<?php

namespace app\Store\controller;
use think\Request;
use think\Validate;
use think\Db;

class Sms extends ApiCommon
{
    
	//发送短信
	public function smssend(){
		$data = input('post.');
		$userid = input('uid/d',0);
		$codes = rand(1000, 9999);
		$data['code'] = $codes;
		
		$phoneNum = db('userinfo_p')->where(['phone'=>$data['mobile']])->count();
		if($phoneNum > 0){
			$this->returnMsg('-1','该手机号码已经被绑定');
		}
		
		$result = sendSms(1,$data['mobile'],$data,$userid);
		
		if($result['status'] == 1){
			$this->returnMsg('ok',$result['msg'].$phoneNum,$result['smsid']);
		}
		else{
			$this->returnMsg('ok',$result['msg']);
		}		
	}
	
	//检测验证码
	public function checkBindPhone(){
		$userid = input('uid');
		$code = input('code');
		$logid = input('smsid');
		
		$userinfo  = db('userinfo')->where(['userid'=>$userid])->field('unionid')->select();
		if(count($userinfo)<=0){
			$this->returnMsg('-1','用户信息错误..');
		}
		
		$result = db('sms_log')->where(['id'=>$logid,'code'=>$code])->select();
		
		if(count($result)>0){
			$sendtime = $result[0]['add_time'];
			if((time()-$sendtime)>300){
				$this->returnMsg('-1','验证超时');
			}
			
			$phones = $result[0]['mobile'];
			db('userinfo_p')->where(['unionid'=>$userinfo[0]['unionid']])->save(['phone'=>$phones]);
			$this->returnMsg('ok','绑定成功');
		}
		else{
			$this->returnMsg('-1','验证码错误');
		}
		
	}
	
}
